﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Camping_Project.Forms
{
    public partial class FormStatistiques : Form
    {
        private  Camping_BDD bdd = new Camping_BDD();
        private double total=0;
        private double tente = 0, cara = 0;
        private int i;

        

        public FormStatistiques()
        {
            InitializeComponent();
            sommeMoyenne();
            moyenneOccupants();
            ratioLogement();
            nbJoursSejour();
            
        }

        private void sommeMoyenne()
        {
            var query =
              from reservation in bdd.Reservation
              where reservation.PayementEffectue == true
              select new
              {
                  reservation.PrixTotal
                  
              };

            i = 0;
            
            foreach (var p in query)
            {
                total += p.PrixTotal;
                i++;
            }
            total /= i;

            tbSomMoy.Text = total.ToString(".##");
        }

        private void moyenneOccupants()
        {
            var query =
              from reservation in bdd.Reservation
              where reservation.PayementEffectue == true
              select new
              {
                  reservation.NbAdultes,
                  reservation.NbEnfants
              };

            double nbMoyOccup=0;
            i = 0;
            foreach (var p in query)
            {
                nbMoyOccup += 2;
                nbMoyOccup += p.NbAdultes;
                nbMoyOccup += p.NbEnfants;
                i++;
            }
            nbMoyOccup /= i;

            tbMoyOcc.Text = nbMoyOccup.ToString(".##");
        }

        private void ratioLogement()
        {
            var query =
             from reservation in bdd.Reservation
             where reservation.PayementEffectue == true
             select new
             {
                 reservation.TypeEmp
             };

            
            foreach (var p in query)
            {
                if (p.TypeEmp == true)
                    cara++;
                else
                    tente++;
            }
            double somme = 0;
            somme = cara + tente;
            cara = cara / somme;
            cara *= 100;

            tente = tente / somme;
            tente *= 100;

            tbTauxTente.Text = tente.ToString(".##");
            tbTauxCara.Text = cara.ToString(".##");
        }

        public void nbJoursSejour()
        {
            var query =
                from reserv in bdd.Reservation
                where reserv.PayementEffectue == true
                select new
                {
                    reserv.DateDebut,
                    reserv.DateFinDef
                };

            double nbTotalJours = 0;
            i = 0;

            foreach (var d in query)
            {
                TimeSpan diff = DateTime.Parse(d.DateFinDef.ToString()) - DateTime.Parse(d.DateDebut.ToString());
                int diffInt = Convert.ToInt32(diff.TotalDays);

                nbTotalJours += diffInt;
                i++;
            }

            nbTotalJours /= i;

            tbMoyJours.Text = nbTotalJours.ToString(".##");
        }

            
    }
}
